home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_fcntl.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  1.4 KB  |  54 lines

  1. #! /usr/bin/env python
  2. """Test program for the fcntl C module.
  3.    Roger E. Masse
  4. """
  5. import struct
  6. import fcntl
  7. import os, sys
  8. from test_support import verbose, TESTFN
  9.  
  10. filename = TESTFN
  11.  
  12. try:
  13.     os.O_LARGEFILE
  14. except AttributeError:
  15.     start_len = "ll"
  16. else:
  17.     start_len = "qq"
  18.  
  19. if sys.platform in ('netbsd1', 'Darwin1.2', 'darwin',
  20.                     'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
  21.                     'bsdos2', 'bsdos3', 'bsdos4',
  22.                     'openbsd', 'openbsd2', 'openbsd3'):
  23.     lockdata = struct.pack('lxxxxlxxxxlhh', 0, 0, 0, fcntl.F_WRLCK, 0)
  24. elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
  25.     lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
  26. else:
  27.     lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
  28. if verbose:
  29.     print 'struct.pack: ', `lockdata`
  30.  
  31.  
  32. # the example from the library docs
  33. f = open(filename, 'w')
  34. rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
  35. if verbose:
  36.     print 'Status from fnctl with O_NONBLOCK: ', rv
  37.  
  38. rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata)
  39. if verbose:
  40.     print 'String from fcntl with F_SETLKW: ', `rv`
  41.  
  42. f.close()
  43. os.unlink(filename)
  44.  
  45.  
  46. # Again, but pass the file rather than numeric descriptor:
  47. f = open(filename, 'w')
  48. rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK)
  49.  
  50. rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
  51.  
  52. f.close()
  53. os.unlink(filename)
  54.